//
//  ZH_GoodsinfoTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/14.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_GoodsinfoTableViewCell.h"
#import "ZH_ShareBoxCollectionViewCell.h"
@interface ZH_GoodsinfoTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
///内容
@property(nonatomic, weak) UICollectionView *collectionView;
///数据
@property(nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic,strong)UILabel *titleLabel;
///选中 Id
@property (nonatomic, strong) NSString *selectId;

@end
@implementation ZH_GoodsinfoTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_GoodsinfoTableViewCell";
    ZH_GoodsinfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_GoodsinfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{    self.selectId = @"";
    [self unShowClickEffect];

}

- (void)loadSubviewConstraints{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(FITSCALE(15));
        make.left.equalTo(self.contentView).offset(FITSCALE(22));
    }];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(FITSCALE(15));
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView);
        //      make.height.mas_equalTo(FITHEIGHT(219));
    }];
}
///更新 ui
- (void)updateUiForData:(NSDictionary *)data
{
    if(data){
        [self.dataArray removeAllObjects];
        self.titleLabel.text = [NSString stringWithFormat:@"%@",data[@"spec_name"]];
        [self.dataArray addObjectsFromArray:data[@"detail"]];
        
        NSInteger item = self.dataArray.count;
        NSInteger rowNum = (item / 3) + ((item % 3) > 0 ? 1 : 0);
        
        CGFloat collectionHeight = FITSCALE(50) * rowNum + (rowNum * FITHEIGHT(0));
        
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(collectionHeight);
        }];
        [self.collectionView reloadData];
    }else{
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(HEIGHT_TRANSFORMATION(0));
        }];
    }
}
#pragma mark -- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.selectId = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]];
    if(self.onClickBlock){
        self.onClickBlock([NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]]);
    }

    [self.collectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGSize size = CGSizeMake((kMainScreenWidth) / 3, FITSCALE(50));
    return size;
}

///设置行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return FITHEIGHT(0);
}

///设置列间距，配合item宽度来设置
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return WIDTH_TRANSFORMATION(0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZH_ShareBoxCollectionViewCell *cell = [ZH_ShareBoxCollectionViewCell collectionView:collectionView cellForItemAtIndexPath:indexPath];
    if(self.dataArray.count > 0){
        [cell updataUI:self.dataArray[indexPath.row]];
    }
    NSString *string = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"id"]];
    if([string isEqualToString:self.selectId]){
        [cell setSelectedState:YES];
    }else{
        [cell setSelectedState:NO];
    }
    
    
    return cell;
}

#pragma mark -- getter,setter

///内容
- (UICollectionView *)collectionView
{
    if(!_collectionView){
        UICollectionView *collectionView = [UICollectionView createCollectionViewWithDelegateTarget:self forScrollDirection:UICollectionViewScrollDirectionVertical];

        [self.contentView addSubview:_collectionView = collectionView];
        //注册
        [ZH_ShareBoxCollectionViewCell registerCollectionView:collectionView];
    }
    return _collectionView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel createLabelWithText:@"" font:kFontSize_28];
        [self.contentView addSubview:_titleLabel = label];
    }
    return _titleLabel;
}
///数据
- (NSMutableArray *)dataArray
{
    if(!_dataArray){
        _dataArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _dataArray;
}


@end
